<?php

/*
 *
 *
 * --------------------------------------------------------------------
 * Copyright (c) 2001 - 2011 Openfiler Project.
 * --------------------------------------------------------------------
 *
 * Openfiler is an Open Source SAN/NAS Appliance Software Distribution
 *
 * This file is part of Openfiler.
 *
 * Openfiler is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * Openfiler is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Openfiler.  If not, see <http://www.gnu.org/licenses/>.
 *
 * --------------------------------------------------------------------
 *
 *
 */

	require("pre.inc");
    include("authenticated.inc");

	global $volumes_id;
	global $volumes_name;
	global $volumes_mountpoint;
	global $volumes_vg;
	global $volumes_fstype;

	$volumes_id = array();
	$volumes_name = array();
	$volumes_mountpoint = array();
	$volumes_vg = array();
	$volumes_fstype = array();

	function volumes_startelement($parser, $name, $attrs)
	{
		global $volumes_id, $volumes_name, $volumes_mountpoint, $volumes_vg, $volumes_fstype;

		if ($name == "VOLUME")
		{
			array_push($volumes_id, $attrs["ID"]);
			array_push($volumes_name, $attrs["NAME"]);
			array_push($volumes_mountpoint, $attrs["MOUNTPOINT"]);
			array_push($volumes_vg, $attrs["VG"]);
			array_push($volumes_fstype, $attrs["FSTYPE"]);
		}
	}

	function volumes_endelement($parser, $name)
	{
	}

	$volumes_parser = xml_parser_create();
	xml_set_element_handler($volumes_parser, "volumes_startelement", "volumes_endelement");
	$volumes_fp = fopen("/opt/openfiler/etc/volumes.xml", "r");

	while ($volumes_data = fread($volumes_fp, 4096))
		xml_parse($volumes_parser, $volumes_data, feof($volumes_fp));

	fclose($volumes_fp);
	xml_parser_free($volumes_parser);


	$snapshots_id = array();
	$snapshots_lvname = array();
	$snapshots_vgname = array();
	$snapshots_shared = array();
	$snapshots_rotateid = array();

	function snapshots_startelement($parser, $name, $attrs)
	{
		global $snapshots_id, $snapshots_lvname, $snapshots_vgname, $snapshots_shared, $snapshots_rotateid;

		if ($name == "SNAPSHOT")
		{
			array_push($snapshots_id, $attrs["ID"]);
			array_push($snapshots_lvname, $attrs["LVNAME"]);
			array_push($snapshots_vgname, $attrs["VGNAME"]);
			array_push($snapshots_shared, $attrs["SHARED"]);
			array_push($snapshots_rotateid, $attrs["ROTATEID"]);
		}
	}

	function snapshots_endelement($parser, $name)
	{
	}

	$snapshots_parser = xml_parser_create();
	xml_set_element_handler($snapshots_parser, "snapshots_startelement", "snapshots_endelement");
	$snapshots_fp = fopen("/opt/openfiler/etc/snapshots.xml", "r");

	while ($snapshots_data = fread($snapshots_fp, 4096))
		xml_parse($snapshots_parser, $snapshots_data, feof($snapshots_fp));

	fclose($snapshots_fp);
	xml_parser_free($snapshots_parser);

	generic_header(array("title" => _("Shares : Snapshot Shares")));
	single_begin(array());

	$curcolor = $GLOBALS["color_table_row2"];
	$imgid = $GLOBALS["color_table_row2_colstr"];

	function getcolor()
	{
		global $curcolor, $imgid;

		if ($curcolor == $GLOBALS["color_table_row1"])
		{
			$curcolor = $GLOBALS["color_table_row2"];
			$imgid = $GLOBALS["color_table_row2_colstr"];
		}
		else
		{
			$curcolor = $GLOBALS["color_table_row1"];
			$imgid = $GLOBALS["color_table_row1_colstr"];
		}

		return $curcolor;
	}

?>

<script src="utility.js" type="text/javascript" language="JavaScript">
</script>
<script src="popup.js" type="text/javascript" language="JavaScript">
</script>

<p>&nbsp;</p>

<?php
        nested_tab_begin("C_SHARES_SNAPSHOTS");
?>
	<div align="center">
	<table cellpadding="0" cellspacing="0" border="0" width="70%">
	<tr>
	<td>
	<div style="border-left: 1px solid rgb(128, 128, 128); border-right: 1px solid rgb(128, 128, 128); border-bottom: 1px solid rgb(128, 128, 128); border-top: 1px solid rgb(128, 128, 128); background-color: <?php print($GLOBALS["color_table_heading"]); ?>; color: <?php print($GLOBALS["color_table_heading_text"]); ?>;">
		<table cellpadding="8" cellspacing="0" border="0">
		<tr>
			<td align="left" valign="middle"><strong>Network Shares Snapshots</strong></td>
		</tr>
		</table>
	</div>
<?php

	global $unique_descriptions, $unique_description_counts;

	$unique_descriptions = array();
	$unique_description_counts = array();

	$dir_type = "";
	$dir_description = "";
	$dir_count = 0;

	global $paddinglevel;
	$paddinglevel = 0;

	function dir_startelement($parser, $name, $attrs)
	{
		global $dir_type, $dir_description, $dir_count;

		if ($name == "KEY")
		{
			if ($attrs["NAME"] == "dirtype")
				$dir_type = $attrs["VALUE"];
			else if ($attrs["NAME"] == "description")
				$dir_description = $attrs["VALUE"];
			else if ($attrs["NAME"] == "dircount")
				$dir_count = $attrs["VALUE"];
		}
	}

	function dir_endelement($parser, $name)
	{
	}

	function process_dir($mountpoint)
	{
		global $paddinglevel, $dir_description, $dir_type, $dir_count, $imgid;

		$paddinglevel++;
		$handle = opendir($mountpoint);

		while ($file = readdir($handle))
		{
			if (($file == ".") || ($file == "..") || (!is_dir($mountpoint . $file)))
				continue;

			if (!is_file($mountpoint . $file . ".info.xml"))
				continue;

			$dir_parser = xml_parser_create();
			xml_set_element_handler($dir_parser, "dir_startelement", "dir_endelement");
			$dir_fp = fopen($mountpoint . $file . ".info.xml", "r");

			while ($dir_data = fread($dir_fp, 4096))
				xml_parse($dir_parser, $dir_data, feof($dir_fp));

			fclose($dir_fp);
			xml_parser_free($dir_parser);

			$rand_id = sha1($file . rand());

?>
			<div style="border-left: 1px solid rgb(128, 128, 128); border-right: 1px solid rgb(128, 128, 128); border-bottom: 1px solid rgb(128, 128, 128); padding-left: <?php print(32 * $paddinglevel); ?>px; background-color: <?php print(getcolor()); ?>; color: rgb(64, 64, 64);">
				<table cellpadding="4" cellspacing="0" border="0">
				<tr>
					<td align="center" valign="middle">
					<?php
						if ($dir_type == "share")
							print("\t<img src=\"/images/icons/folder-share-" . $imgid . ".png\" width=\"22\" height=\"22\" alt=\"\" />\n");
						else if ($dir_type == "leaf")
							print("\t<img src=\"/images/icons/folder-leaf-" . $imgid . ".png\" width=\"22\" height=\"22\" alt=\"\" />\n");
						else if ($dir_type == "closed")
							print("\t<img src=\"/images/icons/folder-closed-" . $imgid . ".png\" width=\"22\" height=\"22\" alt=\"\" />\n");
					?>
					</td>
					<td align="left" valign="middle"><?php print(htmlentities($dir_description)); ?><?php print((1) ? (" (<tt>" . htmlentities($mountpoint . $file . "/") . "</tt>)") : ""); ?></td>
				</tr>
				</table>
			</div>
<?php
			if ($dir_type == "closed")
				process_dir($mountpoint . $file . "/");
		}

		closedir($handle);
		$paddinglevel--;
	}

	function process_volume($volume_index)
	{
		global $imgid;
		global $volumes_id;
		global $volumes_name;
		global $volumes_mountpoint;
		global $volumes_vg;
		global $volumes_fstype;
		global $paddinglevel;

		global $snapshots_id, $snapshots_lvname, $snapshots_vgname, $snapshots_shared, $paddinglevel, $imgid;

		$paddinglevel++;
?>
	<div style="border-left: 1px solid rgb(128, 128, 128); border-right: 1px solid rgb(128, 128, 128); border-bottom: 1px solid rgb(128, 128, 128); padding-left: <?php print(32 * $paddinglevel); ?>px; background-color: <?php print(getcolor()); ?>; color: rgb(64, 64, 64);">
		<table cellpadding="4" cellspacing="0" border="0">
		<tr>
			<td align="center" valign="middle">
				<img src="/images/icons/volume-<?php print($imgid); ?>.png" width="22" height="22" alt="" />
			</td>
			<td align="left" valign="middle">
				<strong><?php print($volumes_name[$volume_index]); ?></strong><?php print((1) ? (" (<tt>" . $volumes_mountpoint[$volume_index] . "</tt>)") : ""); ?>
			</td>
		</tr>
		</table>
	</div>
<?php
		$paddinglevel++;
		for ($i = 0; $i < count($snapshots_id); $i++)
			if (($snapshots_lvname[$i] == $volumes_id[$volume_index]) && ($snapshots_vgname[$i] == $volumes_vg[$volume_index]) && ($snapshots_shared[$i] == "yes"))
			{
?>
			<div style="border-left: 1px solid rgb(128, 128, 128); border-right: 1px solid rgb(128, 128, 128); border-bottom: 1px solid rgb(128, 128, 128); padding-left: <?php print(32 * $paddinglevel); ?>px; background-color: <?php print(getcolor()); ?>; color: rgb(64, 64, 64);">
				<table cellpadding="4" cellspacing="0" border="0">
				<tr>
					<td align="center" valign="middle">
					<?php
						print("\t<img src=\"/images/icons/folder-closed-" . $imgid . ".png\" width=\"22\" height=\"22\" alt=\"\" />\n");
					?>
					</td>
					<td align="left" valign="middle"><strong><em><?php print(htmlentities($snapshots_id[$i])); ?></em></strong><?php print((1) ? (" (<tt>" . htmlentities("/mnt/snapshots/" . $snapshots_vgname[$i] . "/" . $snapshots_lvname[$i] . "/" . $snapshots_id[$i] . "/") . "</tt>)") : ""); ?></td>
				</tr>
				</table>
			</div>
<?php
				process_dir("/mnt/snapshots/" . $snapshots_vgname[$i] . "/" . $snapshots_lvname[$i] . "/" . $snapshots_id[$i] . "/");
			}

		$paddinglevel--;
		$paddinglevel--;
	}

	$vgp = popen("export LANG=C; /usr/bin/sudo " . $GLOBALS["lvm_command_prefix"] . "vgdisplay -c", "r");
	$i = 0;
	while (!feof($vgp))
		$vgds[$i++] = fgets($vgp, 4096);
	pclose($vgp);

	if (count($vgds) <= 0)
		print("<p align=\"center\">There are no existing volume groups.<br />Please create some volume groups.</p>");
	else for ($i = 0; $i < count($vgds); $i++)
	{
		$vginfo = explode(":", trim($vgds[$i], " "));

		if ($vginfo[11] <= 0)
			break;

?>
	<div style="border-left: 1px solid rgb(128, 128, 128); border-right: 1px solid rgb(128, 128, 128); border-bottom: 1px solid rgb(128, 128, 128); padding-left: <?php print(32 * $paddinglevel); ?>px; background-color: <?php print(getcolor()); ?>; color: rgb(64, 64, 64);">
		<table cellpadding="4" cellspacing="0" border="0">
		<tr>
			<td align="center" valign="middle">
				<img src="/images/icons/vg-<?php print($imgid); ?>.png" width="30" height="23" alt="" />
			</td>
			<td align="left" valign="middle">
				<strong><?php print($vginfo[0]); ?></strong><?php print((1) ? (" (<tt>/mnt/" . $vginfo[0] . "/</tt>)") : ""); ?>
			</td>
		</tr>
		</table>
	</div>
<?php
		for ($j = 0; $j < count($volumes_id); $j++)
		{
			if (($volumes_vg[$j] == $vginfo[0]) && ($volumes_fstype[$j] != "iscsi"))
				process_volume($j);
		}
	}

?>
	</td>
	</tr>
	</table>
	</div>
<?php

	nested_tab_end();

	single_end(array());
	generic_footer(array());
?>
